﻿/// <summary>
/// https://leetcode-cn.com/problems/valid-parentheses/
/// </summary>
public class Solution
{
    private readonly Dictionary<char, char> bracketPairs = new Dictionary<char, char>()
    {
        { ')', '(' },
        { ']', '[' },
        { '}', '{' },
    };
    public bool IsValid(string s)
    {
        Stack<char> stack = new Stack<char>();
        foreach (var c in s)
        {
            if (this.bracketPairs.ContainsKey(c))
            {
                if (!stack.TryPop(out char pre) || pre != this.bracketPairs[c])
                    return false;
            }
            else
                stack.Push(c);
        }
        return stack.Count == 0;
    }
}